<template>
  <div class="db2-root">
    <!-- 左侧会话栏 -->
    <aside class="db2-sidebar">
      <div class="db2-sidebar-header">
<!--        <img src="/favicon.ico" class="db2-logo" />-->
        <router-link to="/">
          <span class="db2-title">AI在线课堂</span>
        </router-link>
      </div>
      <a-button type="primary" class="db2-new-btn" @click="newSession">新建会话</a-button>
      <div class="db2-session-list">
        <div
          v-for="session in sessions"
          :key="session.chatId"
          :class="['db2-session-item', {active: session.chatId === activeChatId}]"
          @click="selectSession(session.chatId)"
        >

          <div class="db2-session-title">
            <template v-if="editingChatId === session.chatId">
              <a-input v-model:value="session.title"
                       @pressEnter="()=>saveTitle(session)"
                       autofocus
                       style="width: 100%"
                       @click.stop
                       />
            </template>
            <template v-else>
              {{ session.title }}
            </template>
          </div>
          <div class="db2-session-last">{{ session.messages.length ? session.messages[session.messages.length-1].content : '' }}</div>
          <!-- 三个点下拉菜单 -->
          <a-dropdown
            class="db2-session-actions"
            placement="bottomLeft"
            :trigger="['click']"
          >
            <a class="ant-dropdown-link" @click.prevent>
              <EllipsisOutlined/>
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item class="db2-menu-item-no-padding">
                  <a-button
                    type="text"
                    style="width: 100%; text-align: left;"
                    @click.stop="renameSession2(session)"
                  >
                    <template #icon>
                      <EditOutlined />
                    </template>
                    重命名
                  </a-button>
                </a-menu-item>
                <a-menu-item class="db2-menu-item-no-padding">
                  <a-button
                    type="text"
                    danger
                    style="width: 100%; text-align: left;"
                    @click.stop="deleteSession(session.chatId)"
                  >
                    <template #icon>
                      <delete-outlined />
                    </template>
                    删除
                  </a-button>
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </div>
    </aside>
    <!-- 右侧聊天栏 -->
    <main class="db2-chat-main">
      <AIAssistant ref="aiAssistantRef" />
    </main>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { nextTick, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useChatSessionsStore } from '@/stores/chatSessions'
import { EllipsisOutlined, DeleteOutlined, EditOutlined } from '@ant-design/icons-vue'
import { deleteConversationByChatId, renameSession } from '@/api/chatController.ts'
import { message } from 'ant-design-vue'

const chatStore = useChatSessionsStore()
const { sessions, activeChatId } = storeToRefs(chatStore)
const aiAssistantRef = ref()
// 新建会话
const newSession = () => {
  chatStore.createSession()
}
// 切换会话
const selectSession = (chatId: string) => {
  chatStore.setActive(chatId)
  nextTick(()=>{
    aiAssistantRef.value?.scrollToBottom()
  })
}


const deleteSession = async (chatId) => {
  if (!confirm('确定要删除这个会话吗？')) return
  try {
    const res = await deleteConversationByChatId({ chatId })
    if (res.data.code === 0) {
      const index = chatStore.sessions.findIndex(s => s.chatId === chatId)
      if (index > -1) {
        chatStore.sessions.splice(index, 1)
      }
      if (chatStore.activeChatId === chatId) {
        chatStore.activeChatId = ''
      }
      message.success('删除对话成功')
    } else {
      message.error('删除失败')
    }
  } catch (err) {
    message.error('网络错误')
  }
}


// 重命名
const editingChatId = ref(null)
const renameSession2 = (session) => {
  editingChatId.value = session.chatId
}
const saveTitle = (session) => {
  editingChatId.value = null
  const res = renameSession({
    chatId: session.chatId,
    chatName: session.title
  })
  console.log(res)
}
onMounted(async () => {
  await chatStore.loadConversations()
})
</script>

<script lang="ts">
import { defineComponent } from 'vue'
import AIAssistant from './AIAssistant.vue'
export default defineComponent({
  name: 'AIAssistantLayout',
  components: {
    AIAssistant
  }
})
</script>

<style scoped>
.db2-session-title input {
  padding: 4px 8px;
  font-size: 14px;
}
.db2-root {
  display: flex;
  height: 100vh;
  background: #f5f6fa;
}
.db2-sidebar {
  width: 240px;
  background: #f7fafd;
  border-right: 1.5px solid #e3e8f0;
  display: flex;
  flex-direction: column;
  padding: 0;
}
.db2-sidebar-header {
  display: flex;
  align-items: center;
  padding: 20px 16px 10px 16px;
  border-bottom: 1px solid #e3e8f0;
  background: #fff;
}
.db2-logo {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}
.db2-title {
  font-size: 20px;
  font-weight: 800;
  color: #3578e5;
  letter-spacing: 1px;
}
.db2-session-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
  /* 隐藏滚动条 */
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.db2-session-list::-webkit-scrollbar {
  display: none;
}
.db2-session-item {
  padding: 14px 18px;
  cursor: pointer;
  border-radius: 14px;
  margin: 0 10px 8px 10px;
  transition: background 0.18s, box-shadow 0.18s, border 0.18s;
  color: #222;
  font-size: 15px;
}
.db2-session-item.active, .db2-session-item:hover {
  background: #e3e8f0;
  border-left: 4px solid #3578e5;
  box-shadow: 0 2px 8px 0 rgba(53,120,229,0.04);
}
.db2-session-title {
  font-weight: 700;
  color: #3578e5;
  font-size: 15px;
}
.db2-session-last {
  color: #7a8ca7;
  font-size: 13px;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.db2-new-btn {
  margin: 18px 16px 0 16px;
  border-radius: 20px;
  font-weight: 700;
  width: calc(100% - 32px);
  background: #3578e5;
  border: none;
  color: #fff;
  height: 48px;
  font-size: 16px;
  box-shadow: 0 2px 8px 0 rgba(53,120,229,0.08);
  transition: background 0.18s;
}
.db2-new-btn:hover {
  background: #2256b9;
}
.db2-chat-main {
  flex: 1;
  display: flex;
  align-items: stretch;
  justify-content: center;
  background: #f5f6fa;
  overflow: auto;
  /* 隐藏滚动条 */
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.db2-chat-main::-webkit-scrollbar {
  display: none;
}
.db2-chat-main :deep(.db-bg),
.db2-chat-main :deep(.db-chat-window) {
  background: transparent !important;
  box-shadow: none !important;
  border-radius: 0 !important;
  width: 100% !important;
  max-width: 100% !important;
  min-height: 100vh !important;
  height: 100vh !important;
  margin: 0 !important;
  padding: 0 !important;
}
.db2-session-item {
  position: relative;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid #eee;
  transition: background 0.2s;
}

.db2-session-item.active {
  background-color: #f0f0f0;
}

.db2-session-actions {
  position: absolute;
  right: 10px;
  top: 12px;
  font-size: 16px;
  color: #888;
}

.db2-session-actions:hover {
  color: #333;
}

.db2-menu-item-danger {
  color: red !important;
  display: flex;
  align-items: center;
}

.db2-menu-item-danger svg {
  fill: red !important;
  margin-right: 8px;
}

.db2-menu-item-no-padding .ant-menu-item {
  padding: 0 !important;
}
</style>
